// import parseTime, formatTime and set to filter
export { parseTime, formatTime } from '@/utils'

/**
 * Show plural label if time is plural number
 * @param {number} time
 * @param {string} label
 * @return {string}
 */
function pluralize(time, label) {
  if (time === 1) {
    return time + label
  }
  return time + label + 's'
}

/**
 * @param {number} time
 */
export function timeAgo(time) {
  const between = Date.now() / 1000 - Number(time)
  if (between < 3600) {
    return pluralize(~~(between / 60), ' minute')
  } else if (between < 86400) {
    return pluralize(~~(between / 3600), ' hour')
  } else {
    return pluralize(~~(between / 86400), ' day')
  }
}

/**
 * Number formatting
 * like 10000 => 10k
 * @param {number} num
 * @param {number} digits
 */
export function numberFormatter(num, digits) {
  const si = [
    { value: 1E18, symbol: 'E' },
    { value: 1E15, symbol: 'P' },
    { value: 1E12, symbol: 'T' },
    { value: 1E9, symbol: 'G' },
    { value: 1E6, symbol: 'M' },
    { value: 1E3, symbol: 'k' }
  ]
  for (let i = 0; i < si.length; i++) {
    if (num >= si[i].value) {
      return (num / si[i].value).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + si[i].symbol
    }
  }
  return num.toString()
}

/**
 * 10000 => "10,000"
 * @param {number} num
 */
export function toThousandFilter(num) {
  return (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
}

/**
 * Upper case first char
 * @param {String} string
 */
export function uppercaseFirst(string) {
  return string.charAt(0).toUpperCase() + string.slice(1)
}

/**
 * 将数据库时间转化为正常时间
 * @param {String} time
 * @returns
 */
export function curentTime(time) {
  var d = time ? new Date(time) : new Date()
  var localTime = d.getTime()
  var localOffset = d.getTimezoneOffset() * 60000 // 获得当地时间偏移的毫秒数
  var utc = localTime + localOffset + 3600000 * 8
  var now = new Date(utc)

  var year = now.getFullYear() // 年
  var month = now.getMonth() + 1 // 月
  var day = now.getDate() // 日
  var hh = now.getHours() // 时
  var mm = now.getMinutes() // 分
  var ss = now.getSeconds() // 秒

  var clock = year + '-'

  if (month < 10) { clock += '0' }

  clock += month + '-'

  if (day < 10) { clock += '0' }

  clock += day + ' '

  if (hh < 10) { clock += '0' }

  clock += hh + ':'

  if (mm < 10) { clock += '0' }

  clock += mm + ':'

  if (ss < 10) { clock += '0' }

  clock += ss

  return clock
}

/**
 * 将数据库时间转化为正常日期
 * @param {String} time
 * @returns
 */
export function curentDate(time) {
  var d = time ? new Date(time) : new Date()
  var localTime = d.getTime()
  var localOffset = d.getTimezoneOffset() * 60000 // 获得当地时间偏移的毫秒数
  var utc = localTime + localOffset + 3600000 * 8
  var now = new Date(utc)

  var year = now.getFullYear() // 年
  var month = now.getMonth() + 1 // 月
  var day = now.getDate() // 日

  var clock = year + '-'

  if (month < 10) { clock += '0' }

  clock += month + '-'

  if (day < 10) { clock += '0' }

  clock += day

  return clock
}
